#ifndef COMMON_LOCK_H
#define COMMON_LOCK_H

#include "../Type.h"

#ifdef __WINDOWS__
#include <Windows.h>
#elif __LINUX__

#endif

class MyLock
{
#if defined(__WINDOWS__)

	CRITICAL_SECTION m_Lock ;
public :
	MyLock( ){ InitializeCriticalSection(&m_Lock); } ;
	~MyLock( ){ DeleteCriticalSection(&m_Lock); } ;
	void	Lock( ){ EnterCriticalSection(&m_Lock); } ;
	bool    TryLock( ){ return TryEnterCriticalSection(&m_Lock)? true: false;}
	void	Unlock( ){ LeaveCriticalSection(&m_Lock); } ;

#elif defined(__LINUX__)

	pthread_mutex_t 	m_Lock; 
public :
	MyLock( ){ pthread_mutex_init( &m_Lock , NULL );} ;
	~MyLock( ){ pthread_mutex_destroy( &m_Lock) ; } ;
	void	Lock( ){ pthread_mutex_lock(&m_Lock); } ;
	bool    TryLock( ){ pthread_mutex_trylock( &m_Lock );}
	void	Unlock( ){ pthread_mutex_unlock(&m_Lock); } ;

#endif

	void    RevolveLock( int times = 100)
	{
		for ( int i = 0; i < times; i++ )
		{

			if ( TryLock() )
			{
				return;
			}
		}
		Lock();
	}
};





class MyCondition
{
#if defined(__WINDOWS__)
	CONDITION_VARIABLE conditionVarible;
	CRITICAL_SECTION   criticalSection;
public:
	MyCondition()  { InitializeCriticalSection(&criticalSection);}
	~MyCondition() { DeleteCriticalSection(&criticalSection);    }
#elif defined(__LINUX__)
	pthread_mutex_t 	m_Mutex; 
	pthread_cond_t      m_Cond;
public:
	MyCondition() {pthread_cond_init(&m_Cond,NULL);pthread_mutex_init(&m_Mutex,NULL);}
	~MyCondition(){pthread_cond_init(&m_Cond);     phtread_mutex_destroy(&m_Mutex);  }
#endif

	int Wait(ULONG64 timeout = 0);
	int Free();

	int Get();
	int Notify();
};



#endif